package com.drinks.yiniang.ui.activity.community;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.community.TopicListAdapter;
import com.drinks.yiniang.entity.community.CommunityTopicJson;
import com.drinks.yiniang.io.api.CommunityApiIO;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 话题列表
 *
 * @author han
 * @date 2019/09/10
 */
public class TopicListActivity extends BaseTranBarActivity implements TextView.OnEditorActionListener,
        BaseQuickAdapter.OnItemClickListener {

    public static final int TYPE_TOPIC_LIST = 0x01; //话题列表
    public static final int TYPE_MFRS_BRAND_LIST = 0x02;//厂商列表

    //取消
    private TextView tv_topic_list_cancel;
    //输入框
    private EditText et_topic_list_content;
    //列表
    private BaseRecyclerView rcv_topic_list;

    private TopicListAdapter mTopicListAdapter;

    private int mCurrentType;

    //搜索关键词
    private String mKeywords;

    @Override
    protected void initView() {
        tv_topic_list_cancel = findViewById(R.id.tv_topic_list_cancel);

        et_topic_list_content = findViewById(R.id.et_topic_list_content);

        rcv_topic_list = findViewById(R.id.rcv_topic_list);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mCurrentType = getIntent() != null ? getIntent().getIntExtra(Constants.KeyType, TYPE_TOPIC_LIST) : TYPE_TOPIC_LIST;

        et_topic_list_content.setHint(mCurrentType == TYPE_MFRS_BRAND_LIST ? R.string.text_hint_input_manufacturer : R.string.text_hint_input_topic);

        initAdapter();
        getTopicList();
    }

    private void initAdapter() {
        rcv_topic_list.setLayoutManager(new LinearLayoutManager(mContext));

        mTopicListAdapter = new TopicListAdapter(mContext, new ArrayList<CommunityTopicJson.TopicList>());
        mTopicListAdapter.setCenterEmpty(getLayoutInflater(), rcv_topic_list);
        mTopicListAdapter.bindToRecyclerView(rcv_topic_list);
    }

    @Override
    protected void initListener() {
        tv_topic_list_cancel.setOnClickListener(this);
        et_topic_list_content.setOnEditorActionListener(this);
        mTopicListAdapter.setOnItemClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        if (v.getId() == R.id.tv_topic_list_cancel) {
            finish();
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_topic_list;
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            hideKeyboard(this.getCurrentFocus().getWindowToken());
            mKeywords = StringUtils.getEditTextString(et_topic_list_content.getText());
            //TODO Search
            getTopicList();
        }
        return false;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        CommunityTopicJson.TopicList data = mTopicListAdapter.getItem(position);

        if (data == null) {
            return;
        }

        Intent intent = new Intent();
        intent.putExtra(Constants.KeyObject, data);

        setResult(Constants.RESULT_CODE_NORMAL, intent);
        finish();
    }

    //获取话题列表
    private void getTopicList() {
        //清空数据
        mTopicListAdapter.getData().clear();
        mTopicListAdapter.notifyDataSetChanged();

        if (mCurrentType == TYPE_MFRS_BRAND_LIST) {
            getMfrsList();
            return;
        }

        showLoadingDialog();

        CommunityApiIO.getInstance().getCommunityTopicList(mKeywords, new APIRequestCallback<CommunityTopicJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(CommunityTopicJson res) {
                if (mTopicListAdapter == null) {
                    return;
                }

                mTopicListAdapter.getData().clear();
                mTopicListAdapter.addData(res.getData());
                mTopicListAdapter.loadMoreEnd();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //获取厂商列表
    private void getMfrsList() {
        showLoadingDialog();

        CommunityApiIO.getInstance().getCommunityMfrsList(mKeywords, new APIRequestCallback<CommunityTopicJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(CommunityTopicJson res) {
                if (mTopicListAdapter == null) {
                    return;
                }

                mTopicListAdapter.getData().clear();
                mTopicListAdapter.addData(res.getData());
                mTopicListAdapter.loadMoreEnd();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
